package com.eling.elcms.community.webapp.controller;

import com.eling.elcms.community.model.Building;
import com.eling.elcms.community.service.IBuildingManager;
import com.eling.elcms.core.util.PropertyUtils;
import com.eling.elcms.core.webapp.controller.BaseFormController;
import com.eling.elcms.core.webapp.controller.Msg;
import com.eling.elcms.privilege.service.IRbacService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class BuildingController extends BaseFormController{
    @Autowired
    private IBuildingManager buildingManager;
    @Autowired
    private IRbacService rbacService;
    
    @RequestMapping("/api/building/add")
    @ResponseBody
    public Building add(@ModelAttribute Building building) {  
    	return buildingManager.save(building);
    }
    @RequestMapping("/api/building/{pkBuilding}/delete")
    @ResponseBody
    public Msg delete(@PathVariable Long pkBuilding) {
    	buildingManager.remove(pkBuilding);
    	return new Msg("删除成功");
    }
    
    @RequestMapping("/api/building/update")
    @ResponseBody
    public Building update(@RequestBody Building building) {
    	return buildingManager.save(building);
    }
    
    @RequestMapping("/api/building/query")
    @ResponseBody
    public List<Building> query(@ModelAttribute Building cond) {
    	return buildingManager.query(cond);
    }

    @RequestMapping("/api/buildings")
    @ResponseBody
    public List<Building> queryAll() {
    	Building cond = new Building();
    	PropertyUtils.setProperty(cond, "validatePrivilege", Boolean.FALSE);
    	return buildingManager.query(cond);
    }
    
    @RequestMapping("/api/building/getuserauthbuilding")
    @ResponseBody
    public List<Building> getUserAuthBuilding(@RequestParam("pkUser") Long pkUser) {
    	Building cond = new Building();
    	cond.setUseType(Building.UseType.Apartment);
    	PropertyUtils.setProperty(cond, "pkBuildingIn", rbacService.getUserAssignment(pkUser, "building"));
    	return query(cond);
    }
}
